<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 8/30/12
 * Time: 10:48 AM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Cms_PageTypes extends Page
{
    /**
     * @var Db_Cms_PageTypes
     */
    protected $tableObject;

    function __construct() {
        $this->idTableObject = get('id', 'int');
        $this->actionsTableObject = array(
            'addEditAction',
            'saveAction',
            'deleteAction'
        );
        $this->classTableObject = 'Db_Cms_PageTypes';
        parent::__construct();
    }

    public function defaultAction() {
        if (perm('PAGE_TYPES_EDIT')) {
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
        }
        self::$TPL->appendJsFile('admin/page_types');
        $allPageTypes = Db_Cms_PageTypes::getAllPageTypes();
		self::$TPL->assign('allPageTypes', $allPageTypes);
        self::$TPL->setBodyTemplate('list_page_types');
        return array(
            'type' => 'display'
        );
    }

    public function addEditAction() {
        perm('PAGE_TYPES_EDIT', self::$PAGE['p_url']);
		if(!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' &quot;' . t( T_PAGE_TYPES_TITLE ,$this->tableObject->objectArray->pt_title) . '&quot;', '?action=addEdit&id=' . $this->idTableObject, true);
			self::$TPL->assign('pageTypes', $this->tableObject->objectArray);
		} else {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
		}
		self::$TPL->setBodyTemplate('add_edit_page_type');
		return array(
			'type' => 'display'
		);
    }

    public function saveAction() {
		perm('PAGE_TYPES_EDIT', self::$PAGE['p_url']);
        $pageTypeName = strtolower(post('pt_title', 'string'));
        $this->message = t(T_FORM_MSG, 'success');
        if(!empty($pageTypeName)){
            if(!empty($this->tableObject->objectArray->id)) {
                $this->tableObject->id = $this->idTableObject;
            }
            $this->tableObject->pt_title = $pageTypeName;
            $this->tableObject->pt_show_header = post('pt_show_header');
            $this->tableObject->pt_alt_header = post('pt_alt_header', 'string');
            $this->tableObject->pt_show_footer = post('pt_show_footer');
            $this->tableObject->pt_alt_footer = post('pt_alt_footer', 'string');

            if(!$this->tableObject->save()) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

    public function deleteAction() {
		perm('PAGE_TYPES_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');
            if(!Db_Cms_PageTypes::abstractDelete($this->idTableObject)) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }
}
